package ${package.Controller};

import com.hbsj.core.config.Constants;
import com.hbsj.core.entity.MapResponse;
import com.hbsj.core.entity.PageResponse;
import com.hbsj.core.page.PageEntity;
import com.hbsj.core.page.PageKit;
import com.hbsj.core.page.PageResult;
import ${package.Entity}.*;
import ${package.Service}.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.math.BigDecimal;

@RestController
@RequestMapping("/${entity?lower_case}")
@Validated
public class ${entity}Controller {
    @Autowired
    private ${entity}Service ${entity?uncap_first}Service;

    @PostMapping("/create")
    public MapResponse create(
<#list table.fieldsWithoutIdAndCU as field>
        <#if field.propertyType == 'Integer' || field.propertyType == 'Long' || field.propertyType == 'BigDecimal' || field.propertyType == 'Date'>@NotNull(message = "${field.comment}不能为空")<#else>@NotEmpty(message = "${field.comment}不能为空")</#if><#if field.propertyType == 'Date'>@DateTimeFormat(pattern = Constants.DATE_FORMAT1)</#if>${field.propertyType} ${field.propertyName}<#if field_index != table.fieldsWithoutIdAndCU?size-1>,</#if>
</#list>
    ) {
        ${entity} ${entity?uncap_first}Created = ${entity?uncap_first}Service.create(
<#list table.fieldsWithoutIdAndCU as field>
                ${field.propertyName}<#if field_index != (table.fieldsWithoutIdAndCU?size-1)>,</#if>
</#list>
        );

        return new MapResponse()
                .setData(${entity?uncap_first}Created, ${entity}.class);
    }

    @PostMapping("/update")
    public MapResponse update(
<#list table.fieldsWithoutCU as field>
    <#if field.propertyType == 'Integer' || field.propertyType == 'Long' || field.propertyType == 'BigDecimal'|| field.propertyType == 'Date'>@NotNull(message = "${field.comment}不能为空")<#else>@NotEmpty(message = "${field.comment}不能为空")</#if><#if field.propertyType == 'Date'>@DateTimeFormat(pattern = Constants.DATE_FORMAT1)</#if>${field.propertyType} ${field.propertyName}<#if field_index != table.fieldsWithoutCU?size-1>,</#if>
</#list>
    ) {
        ${entity} ${entity?uncap_first}Updated = ${entity?uncap_first}Service.update(
<#list table.fieldsWithoutCU as field>
                ${field.propertyName}<#if field_index != (table.fieldsWithoutCU?size-1)>,</#if>
</#list>
        );

        return new MapResponse()
                .setData(${entity?uncap_first}Updated, ${entity}.class);
    }

    @PostMapping("/del")
    public MapResponse del(long id) {
        ${entity?uncap_first}Service.delete(id);

        return MapResponse.INSTANCE;
    }

    @GetMapping("/get")
    public MapResponse get(long id) {
        ${entity} ${entity?uncap_first}Get = ${entity?uncap_first}Service.get(id);

        return new MapResponse()
                .setData(${entity?uncap_first}Get, ${entity}.class);
    }

    @GetMapping("/getpagelist")
    public PageResponse getPageList(
            int current,
            int pagesize
    ) {
        PageEntity pageEntity = new PageEntity(current, pagesize);
        PageResult<${entity}> pageResult = ${entity?uncap_first}Service.getPagelist(pageEntity);

        return PageKit.toPageResponse(pageResult);
    }

}
